package layui.controller.product;

import layui.controller.BaseServlet;
import layui.dao.ProductDao;
import layui.dao.impl.ProductDaoImpl;
import layui.entity.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @Author lzp
 * @Date 2023-11-22
 */
@WebServlet("/admin/product/product_add")
public class AddProductServlet extends BaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String productId = req.getParameter("productId");
        String productName = req.getParameter("productName");
        String productType = req.getParameter("productType");
        double productPrice = Double.parseDouble(req.getParameter("productPrice"));
        int  productStatus = Integer.parseInt(req.getParameter("productStatus"));
        String productDesc = req.getParameter("productDesc");

        Product p= new Product();
        p.setProductId(productId);
        p.setProductName(productName);
        p.setProductType(productType);
        p.setProductPrice(productPrice);
        p.setProductStatus(productStatus);
        p.setProductDesc(productDesc);
        p.setProductDate(new Date());

        ProductDao productDao =new ProductDaoImpl();
        int row = productDao.add(p);

        if (row==1){
            print(resp,successJson(null));
            return;
        }
        print(resp,errorJson());
    }
}
